package com.xiaozhaovip.demo.controller;

import com.xiaozhaovip.demo.intf.DO.ResponseDO;
import com.xiaozhaovip.demo.intf.entity.YueQiLaiActivityCollect;
import com.xiaozhaovip.demo.intf.entity.YueQiLaiInfo;
import com.xiaozhaovip.demo.intf.service.YueQiLaiActivityCollectService;
import com.xiaozhaovip.demo.intf.service.YueQiLaiInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Cauli
 */
@Slf4j
@RestController
@RequestMapping("/yueQiLai/activity/collect")
public class YueQiLaiActivityCollectController extends BaseController {
    @Resource
    private YueQiLaiInfoService yueQiLaiInfoService;

    @Resource
    private YueQiLaiActivityCollectService yueQiLaiActivityCollectService;


    /**
     * 模拟当前登录用户
     */
    final int currentUserId = 2;

    /**
     * 改变用户的收藏状态，增加活动的收藏数量
     *
     * @param activityId
     * @param collectStatue
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/userCollect")
    public ResponseDO userCollect(int activityId, int collectStatue) {
        // 参数验证
        if (currentUserId < 1) {
            return new ResponseDO(false, "请登录后再操作！", FAIL_CODE, currentUserId);
        }
        if (activityId < 1) {
            return new ResponseDO(false, "活动id错误！", FAIL_CODE, activityId);
        }
        log.info("collect activity,"+currentUserId,"YueQiLaiInfo,"+activityId);
        YueQiLaiInfo activityInfo = yueQiLaiInfoService.loadById(activityId);
        if (activityInfo == null) {
            return new ResponseDO(false, "活动不存在！", FAIL_CODE, activityId);
        }
        if (collectStatue > 1 || collectStatue < 0) {
            return new ResponseDO(false, "收藏状态错误", FAIL_CODE, collectStatue);
        }

        // 查询收藏记录
        YueQiLaiActivityCollect activityCollect = yueQiLaiActivityCollectService.loadByUserIdAndActivityId(currentUserId, activityId);

        if (collectStatue == 0) {   // 未收藏，需要收藏
            getActivityCollect(activityId, currentUserId, activityCollect, 1);
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "活动收藏成功！");
        } else {    // 已收藏，需要取消收藏
            getActivityCollect(activityId, currentUserId, activityCollect, 0);
            return new ResponseDO(true, SUCCESS, SUCCESS_CODE, "活动取消收藏成功！");
        }
    }

    /**
     * 判断数据库是否有收藏活动的记录，有则修改，没有则新增
     *
     * @param activityId
     * @param userId
     * @param activityCollect
     */
    private void getActivityCollect(int activityId, int userId, YueQiLaiActivityCollect activityCollect, int newStatus) {
        if (activityCollect == null) {  // 未有收藏活动记录，新增记录
            activityCollect = new YueQiLaiActivityCollect();
            activityCollect.setActivityId(activityId);
            activityCollect.setUserId(userId);
            activityCollect.setStatus(newStatus);
            yueQiLaiActivityCollectService.insert(activityCollect);
        } else if (newStatus != activityCollect.getStatus()) {  // 已有收藏活动记录，要修改的状态与原状态不同，修改记录
            yueQiLaiActivityCollectService.updateStatusById(userId, activityId, newStatus);
        } else {    // 已有收藏活动记录，要修改的状态与原状态相同，不做任何操作
            return;
        }

        if (newStatus == 1) {   // 增加活动的收藏数量
            yueQiLaiInfoService.incrCollectNumById(activityCollect.getActivityId());
        } else {    // 减少活动的收藏数量
            yueQiLaiInfoService.decrCollectNumById(activityCollect.getActivityId());
        }
    }
}
